﻿/*
 * Rappelz: Endless Odyssey - The first free open source Rappelz server emulator
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rappelz.GameServer.Network;
using System.Data.Common;
using System.Data;
using Rappelz.GameServer.Network.GameClient;

namespace Rappelz.GameServer.Database
{
    public class DB_OnStartUp : IDBProc
    {
        BaseObject m_CBObject;


        public DB_OnStartUp()
        {
        }

        public void SetCB(BaseObject obj)
        {
            m_CBObject = obj;
        }

        public void RunCB()
        {
            if (m_CBObject != null)
                m_CBObject.onEndQuery();
        }

        public bool onProcess()
        {
            DbConnection conn = GameDBManager.Instance.CreateConnection();
            callStartupHandler(conn);
            readMaxItemIndex(conn);
            readMaxSkillIndex(conn);
            readMaxSummonIndex(conn);
            readMaxPetIndex(conn);
            //removeDeletedCharacter(conn);
            return true;
        }

        private bool callStartupHandler(DbConnection conn)
        {
            DbCommand cmd = conn.CreateCommand();

            cmd.CommandText = "on_server_startup";
            cmd.CommandType = CommandType.StoredProcedure;
            DbParameter out_sid = GameDBManager.Instance.CreateOutParameter(cmd, "OUT_SID", DbType.Int32);

            conn.Open();
            cmd.ExecuteScalar();

            conn.Close();

            return true;
        }

        private bool readMaxItemIndex(DbConnection conn)
        {
            DbCommand cmd = conn.CreateCommand();

            cmd.CommandText = "select MAX(sid) as max_id from Item with (nolock) where sid < 0x3FFFFFFFFFFFFFFF";
            cmd.CommandType = CommandType.Text;

            conn.Open();
           
            try
            {
                long count = Marshal.ToInt64(cmd.ExecuteScalar(), 0);
                if (count == 0)
                    count = 2;

                Player.SetMaxItemUID(count);
            }
            catch (System.Exception ex)
            {
                Globals.Log.Error("DB_OnStartUp.readMaxItemIndex error: {0}", ex);
            }

            conn.Close();
            return true;
        }

        private bool readMaxSkillIndex(DbConnection conn)
        {
            DbCommand cmd = conn.CreateCommand();

            cmd.CommandText = "select MAX(sid) as max_id from Skill with (nolock) where sid < 0x3FFFFFFF";
            cmd.CommandType = CommandType.Text;

            conn.Open();

            try
            {
                int count = Marshal.ToInt32(cmd.ExecuteScalar(), 0);
                if (count == 0)
                    count = 2;

                Creature.SetMaxSkillUID(count);
            }
            catch (System.Exception ex)
            {
                Globals.Log.Error("DB_OnStartUp.readMaxSkillIndex error: {0}", ex);
            }

            conn.Close();
            return true;
        }

        private bool readMaxSummonIndex(DbConnection conn)
        {
            DbCommand cmd = conn.CreateCommand();

            cmd.CommandText = "select MAX(sid) as max_id from Summon with (nolock)";
            cmd.CommandType = CommandType.Text;
            conn.Open();

            try
            {
                int count = Marshal.ToInt32(cmd.ExecuteScalar(),0);
                if (count == 0)
                    count = 2;
                Player.SetMaxSummonSID(count);
            }
            catch (System.Exception ex)
            {
                Globals.Log.Error("DB_OnStartUp.readMaxSummonIndex error: {0}", ex);
            }

            conn.Close();
            return true;
        }

        private bool readMaxPetIndex(DbConnection conn)
        {
            DbCommand cmd = conn.CreateCommand();

            cmd.CommandText = "select MAX(sid) as max_id from Pet with (nolock) where sid < 0x3FFFFFFF";
            cmd.CommandType = CommandType.Text;

            conn.Open();

            try
            {
                long count = Marshal.ToInt64(cmd.ExecuteScalar(), 0);
                if (count == 0)
                    count = 2;

                Player.SetMaxPetSID(count);
            }
            catch (System.Exception ex)
            {
                Globals.Log.Error("DB_OnStartUp.readMaxPetIndex error: {0}", ex);
            }

            conn.Close();
            return true;
        }

    }

}
